#include <linux/init.h>
#include <linux/module.h>
#include <linux/sysfs.h>
#include <linux/kernel.h>
#include <linux/kobject.h>
#include <linux/stat.h>
#define DATESIEZ 1024
static struct kobject my_kobj;
char date[DATESIEZ];
static int  date_len;
void my_release(struct kobject *kobj)
{
    printk(KERN_INFO "release \n");
}
ssize_t my_show(struct kobject *kobj, struct attribute *attr, char *buff)
{
    printk("show:%s\n ",date);
    return sprintf(buff,"%s",date);
}
ssize_t my_store(struct kobject *kobj, struct attribute *attr, const char *buff, size_t n)
{
    //int count = 0;
   // while(date_len<=DATESIEZ&&count<=n)
        //while(date_len<=DATESIEZ&&buff[count]!='\0')
    //{
    //    date[date_len++] = buff[count++];
    //}
    //date_len+=n;
    //
    //printk("store %s\n",date);
    //return n;    
    // return count;
     return sprintf(date,"%s\n",buff);
}
// struct sysfs_ops {
// 	ssize_t	(*show)(struct kobject *, struct attribute *, char *);
// 	ssize_t	(*store)(struct kobject *, struct attribute *, const char *, size_t);
// };
/*读写操作*/
static struct sysfs_ops my_ops = {
    .show = my_show,
    .store = my_store,
};
/*
   struct attribute {
   const char		*name;
   umode_t			mode;
#ifdef CONFIG_DEBUG_LOCK_ALLOC
bool			ignore_lockdep:1;
struct lock_class_key	*key;
struct lock_class_key	skey;
#endif
};*/
static struct attribute attrs = {
    .name = "ljl",
    .mode = S_IRWXUGO //
};
static struct attribute *my_attr[] = {
    &attrs,
    NULL};
/*
   struct kobj_type {
   void (*release)(struct kobject *kobj);
   const struct sysfs_ops *sysfs_ops;
   struct attribute **default_attrs;	 #use default_groups instead
   const struct attribute_group **default_groups;
   const struct kobj_ns_type_operations *(*child_ns_type)(struct kobject *kobj);
   const void *(*namespace)(struct kobject *kobj);
   void (*get_ownership)(struct kobject *kobj, kuid_t *uid, kgid_t *gid);
   };
   */
/*设备操作*/
static struct kobj_type my_type = {
    .release = my_release,   //类似与智能指针
    .sysfs_ops = &my_ops,    //设备读写
    .default_attrs = my_attr //设备操作属性
};
static int init_sys(void)
{
    date_len = 0;
    int ret = 0;
    ret = kobject_init_and_add(&my_kobj, &my_type, NULL, "SYSRW");
    if (ret)
    {
        /*安装失败，删除对象*/
        kobject_del(&my_kobj);
        printk(KERN_INFO "安装失败\n");
        return -1;
    }
    printk(KERN_INFO "安装成功\n");
    return 0;
}
static void exit_sys(void)
{
    kobject_del(&my_kobj);
    /*对象引用计数自减，为0结束*/
    kobject_put(&my_kobj);
    printk("卸载成功\n");
}

module_init(init_sys);
module_exit(exit_sys);

MODULE_LICENSE("GPL");
